package name.marz.to.go.ahg.mhbk.executer;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import name.marz.to.go.ahg.common.util.HttpUtil;
import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.Parameters;
import name.marz.to.go.ahg.core.checker.Checker;
import name.marz.to.go.ahg.core.executer.AbstractPostExecuter;
import name.marz.to.go.ahg.core.responsehandler.DocumentResponseHandler;

import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.nodes.Document;

public class MHBKSecretQAExecuter extends AbstractPostExecuter<Document> {
	private Document inDoc;
	private String answer;
	private Checker<Document> checker = new MHBKSecretQAChecker();
	
	@Deprecated
	public MHBKSecretQAExecuter(Document doc,AutoGetterHttpClient client) {
		super(HttpUtil.createURISafety(doc.getElementsByTag("form").select("[name=FORM1]").first().attr("action")), client,"QA");
		this.inDoc = doc;
	}
	public MHBKSecretQAExecuter(URI uri, AutoGetterHttpClient client,Document doc) {
		super(uri, client,"QA");
		this.inDoc = doc;
	}
	
	public void setAnswer(String answer) {
		this.answer = answer;
	}

	@Override
	protected ResponseHandler<Document> getResponseHandler() {
		return new DocumentResponseHandler(client.getDefaultCharset());
	}

	@Override
	protected List<NameValuePair> getRequestParameters() {
		Parameters p = HttpUtil.getParametersFromInputTags(inDoc);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("txbTestWord", answer));
		params.add(p.getNameValuePair("_FORMID"));
		params.add(p.getNameValuePair("POSTKEY"));
		params.add(p.getNameValuePair("_TARGETID"));
		params.add(p.getNameValuePair("_TOKEN"));
		params.add(p.getNameValuePair("_SUBINDEX"));
		params.add(p.getNameValuePair("_FRAMEID"));
		return params;
	}

	@Override
	protected Checker<Document> getChecker() {
		return checker;
	}
}